package com.hy.dao;

import com.hy.bean.Dispose;
import com.hy.utils.DBManager;
import com.hy.vo.DisposeReporeVo;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.util.List;

/**
 *
 *
 * @Description: 处理dao
 * @Author: zhangzhengwei2006
 * @Date: 2024-01-15 17:19
 *
 *
 */
public class DisposeDao {
    private QueryRunner queryRunner = new QueryRunner();

    // 添加处理
    public int saveDispose(Dispose dispose) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "insert into dispose values (null,?,?,?,?)";
        int update = queryRunner.update(conn, sql, dispose.getUid(), dispose.getDisposeType(), dispose.getDisposeDate(), dispose.getCreationTime());
        DbUtils.close(conn);
        return update;
    }

    // 查询所有被处理的用户
    public List<DisposeReporeVo> selectList() throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select distinct di.*, re.id as 'rid' from report re,dispose di where re.umsuserid = di.uid and re.reportingType = '0' and re.restate = '1' and di.disposeType != '2' and re.tombstone != '1' group by re.umsuserid";
        List<DisposeReporeVo> query = queryRunner.query(conn, sql, new BeanListHandler<>(DisposeReporeVo.class));
        DbUtils.close(conn);
        return query;
    }

    public int deleteBydid(Integer uid,Connection conn) throws Exception {
//        Connection conn = DBManager.getConn();
//        conn.setAutoCommit(false);
        String sql = "delete from dispose where uid = ? ";
        int update = queryRunner.update(conn, sql, uid);
//        DbUtils.close(conn);
        return update;
    }

    public Dispose selectByUid(Integer uid) throws Exception {
        Connection conn = DBManager.getConn();
        String sql = "select * from dispose where uid = ? order by disposeDate limit 0,1";
        Dispose query = queryRunner.query(conn, sql, new BeanHandler<>(Dispose.class), uid);
        DbUtils.close(conn);
        return query;
    }
}
